package org.lex.keytool.ui.common;

import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Insets;

import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;

import org.lex.keytool.Swings;
import org.lex.keytool.core.DName;

public class DNamePane extends JPanel {

	public static void main(String[] args) {
		Swings.show(new DNamePane());
	}

	private JLabel commonNameLabel;
	private JLabel orgUnitLabel;
	private JLabel orgNameLabel;
	private JLabel localNameLabel;
	private JLabel stateNameLabel;
	private JLabel countryLabel;
	private JTextField commonNameTextField;
	private JTextField orgUnitTextField;
	private JTextField orgNameTextField;
	private JTextField localNameTextField;
	private JTextField stateNameTextField;
	private JTextField countryTextField;

	private DName dname = null;

	public DNamePane() {
		this(new DName());
	}

	public DNamePane(DName dname) {
		if (null == dname) {
			dname = new DName();
		}
		this.dname = dname.clone();
		initComponents();
		this.setDName(dname);
	}

	public void setDName(DName dname) {
		if (null == dname) {
			dname = new DName();
		}
		dname = dname.clone();
		this.dname = dname;
		this.commonNameTextField.setText(dname.get(DName.Name.CN));
		this.orgUnitTextField.setText(dname.get(DName.Name.OU));
		this.orgNameTextField.setText(dname.get(DName.Name.O));
		this.localNameTextField.setText(dname.get(DName.Name.L));
		this.stateNameTextField.setText(dname.get(DName.Name.S));
		this.countryTextField.setText(dname.get(DName.Name.C));
	}

	public DName getDName() {
		dname.set(DName.Name.CN, this.commonNameTextField.getText());
		dname.set(DName.Name.OU, this.orgUnitTextField.getText());
		dname.set(DName.Name.O, this.orgNameTextField.getText());
		dname.set(DName.Name.L, this.localNameTextField.getText());
		dname.set(DName.Name.S, this.stateNameTextField.getText());
		dname.set(DName.Name.C, this.countryTextField.getText());
		return dname.clone();
	}

	private void initComponents() {
		GridBagConstraints gc;

		setLayout(new GridBagLayout());

		commonNameLabel = new JLabel("Common Name");
		gc = new GridBagConstraints();
		gc.gridx = 0;
		gc.gridy = 0;
		gc.anchor = GridBagConstraints.BASELINE_LEADING;
		gc.insets = new Insets(0, 0, 5, 5);
		add(commonNameLabel, gc);

		commonNameTextField = new JTextField("Common Name");
		gc = new GridBagConstraints();
		gc.gridy = 0;
		gc.fill = GridBagConstraints.HORIZONTAL;
		gc.anchor = GridBagConstraints.BASELINE_LEADING;
		gc.weightx = 10.0;
		gc.insets = new Insets(0, 0, 5, 0);
		add(commonNameTextField, gc);

		orgUnitLabel = new JLabel("Organization Unit");
		gc = new GridBagConstraints();
		gc.gridx = 0;
		gc.gridy = 1;
		gc.anchor = GridBagConstraints.BASELINE_LEADING;
		gc.insets = new Insets(0, 0, 5, 5);
		add(orgUnitLabel, gc);

		orgUnitTextField = new JTextField("Organization Unit");
		gc = new GridBagConstraints();
		gc.gridy = 1;
		gc.fill = GridBagConstraints.HORIZONTAL;
		gc.anchor = GridBagConstraints.BASELINE_LEADING;
		gc.weightx = 10.0;
		gc.insets = new Insets(0, 0, 5, 0);
		add(orgUnitTextField, gc);

		orgNameLabel = new JLabel("Organization Name");
		gc = new GridBagConstraints();
		gc.gridx = 0;
		gc.gridy = 2;
		gc.anchor = GridBagConstraints.BASELINE_LEADING;
		gc.insets = new Insets(0, 0, 5, 5);
		add(orgNameLabel, gc);

		orgNameTextField = new JTextField("Organization Name");
		gc = new GridBagConstraints();
		gc.gridy = 2;
		gc.fill = GridBagConstraints.HORIZONTAL;
		gc.anchor = GridBagConstraints.BASELINE_LEADING;
		gc.weightx = 10.0;
		gc.insets = new Insets(0, 0, 5, 0);
		add(orgNameTextField, gc);

		localNameLabel = new JLabel("Locality Name");
		gc = new GridBagConstraints();
		gc.gridx = 0;
		gc.gridy = 3;
		gc.anchor = GridBagConstraints.BASELINE_LEADING;
		gc.anchor = GridBagConstraints.BASELINE_LEADING;
		gc.insets = new Insets(0, 0, 5, 5);
		add(localNameLabel, gc);

		localNameTextField = new JTextField("Locality Name");
		gc = new GridBagConstraints();
		gc.gridy = 3;
		gc.fill = GridBagConstraints.HORIZONTAL;
		gc.anchor = GridBagConstraints.BASELINE_LEADING;
		gc.weightx = 10.0;
		gc.insets = new Insets(0, 0, 5, 0);
		add(localNameTextField, gc);

		stateNameLabel = new JLabel("State Name");
		gc = new GridBagConstraints();
		gc.gridx = 0;
		gc.gridy = 4;
		gc.anchor = GridBagConstraints.BASELINE_LEADING;
		gc.anchor = GridBagConstraints.BASELINE_LEADING;
		gc.insets = new Insets(0, 0, 5, 5);
		add(stateNameLabel, gc);

		stateNameTextField = new JTextField("State Name");
		gc = new GridBagConstraints();
		gc.gridx = 1;
		gc.fill = GridBagConstraints.HORIZONTAL;
		gc.anchor = GridBagConstraints.BASELINE_LEADING;
		gc.weightx = 10.0;
		gc.insets = new Insets(0, 0, 5, 0);
		add(stateNameTextField, gc);

		countryLabel = new JLabel("Country");
		gc = new GridBagConstraints();
		gc.gridx = 0;
		gc.gridy = 5;
		gc.anchor = GridBagConstraints.BASELINE_LEADING;
		gc.insets = new Insets(0, 0, 0, 5);
		add(countryLabel, gc);

		countryTextField = new JTextField("Country");
		gc = new GridBagConstraints();
		gc.gridx = 1;
		gc.fill = GridBagConstraints.HORIZONTAL;
		gc.anchor = GridBagConstraints.BASELINE_LEADING;
		gc.weightx = 10.0;
		add(countryTextField, gc);
	}
}
